package com.douqu.game.main.msg;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bean.core.buffer.ByteBuffer;
import com.bean.core.util.TimeUtils;
import com.douqu.game.core.config.challenge.WorldBossConfig;
import com.douqu.game.core.controller.PlayerController;
import com.douqu.game.core.database.model.FeedbackModel;
import com.douqu.game.core.database.model.GMModel;
import com.douqu.game.core.database.model.PlayerModel;
import com.douqu.game.core.e.E_AuctionSource;
import com.douqu.game.core.e.E_CityInOutType;
import com.douqu.game.core.e.E_CityInitGarrisonType;
import com.douqu.game.core.e.E_GMType;
import com.douqu.game.core.entity.Player;
import com.douqu.game.core.entity.battle.BattleDetail;
import com.douqu.game.core.entity.common.TwoTuple;
import com.douqu.game.core.entity.world.WorldAuctionBean;
import com.douqu.game.core.entity.world.WorldAuctionData;
import com.douqu.game.core.entity.world.WorldBossData;
import com.douqu.game.core.entity.world.WorldRankBean;
import com.douqu.game.core.entity.world.map.*;
import com.douqu.game.core.factory.CodeFactory;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.factory.WorldInfoFactory;
import com.douqu.game.core.netty.NettyConnection;
import com.douqu.game.core.protobuf.SGCommonProto;
import com.douqu.game.core.util.LogUtils;
import com.douqu.game.core.util.StringUtils;
import com.douqu.game.main.GameServer;
import com.douqu.game.main.database.mapper.read.ReadFeedbackMapper;
import com.douqu.game.main.database.mapper.read.ReadPlayerMapper;
import com.douqu.game.main.server.ServerManager;
import com.douqu.game.main.server.SpringContext;
import com.douqu.game.main.server.entity.ServerInfoBattle;
import com.douqu.game.main.service.PlayerService;

import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @Author: Bean
 * @Description:
 * @Date: 2018-10-18 10:56
 */
public class GMDataChannel {

    public static void messageChannel(int code, NettyConnection connection, ByteBuffer data) throws Exception
    {
        switch (code)
        {
            case CodeFactory.GM_DATA_BATTLE_LIST:
                battleList(connection, data, code);
                break;
            case CodeFactory.GM_DATA_BATTLE_DETAIL:
                battleServer(connection, data, code);
                break;
            case CodeFactory.GM_DATA_FEEDBACK_LIST:
                feedbackList(connection, data, code);
                break;
            case CodeFactory.GM_DATA_BOSS_LIST:
                bossList(connection, code);
                break;
            case CodeFactory.GM_DATA_AUCTION_LIST:
                auctionList(connection, data, code);
                break;
            case CodeFactory.GM_DATA_AUCTION_DETAIL:
                auctionDetails(connection, data, code);
                break;
            case CodeFactory.GM_DATA_MAP_CITY_LIST:
                cityList(connection, data, code);
                break;
            case CodeFactory.GM_DATA_MAP_CITY_DETAIL:
                cityDetail(connection, data, code);
                break;
            case CodeFactory.GM_DATA_FEEDBACK_EXPORT:
                feedbackExport(connection, data, code);
                break;
        }
    }

    private static void feedbackExport(NettyConnection connection, ByteBuffer data, int code)
    {
        int sendCount = data.readInt();
        ReadFeedbackMapper feedBackMapper = SpringContext.getBean(ReadFeedbackMapper.class);
        List<FeedbackModel> feedbackModelList = feedBackMapper.findAll();
        JSONObject response = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject = null;
        for (FeedbackModel feedbackModel : feedbackModelList) {
            jsonObject = new JSONObject();
            jsonObject.put("id", feedbackModel.getId());
            jsonObject.put("playerName", feedbackModel.getPlayerIndex() + "-" + feedbackModel.getPlayerName());
            jsonObject.put("content", feedbackModel.getContent());
            jsonObject.put("createTime", TimeUtils.getFormatTime(feedbackModel.getCreateTime().getTime()));
            jsonArray.add(jsonObject);
        }

        response.put("data", jsonArray);

        String content = response.toJSONString();
        List<String> list = new ArrayList<>();
        int size = content.length() / sendCount;
        for(int i = 0; i < size; i++)
        {
            list.add(content.substring(i*sendCount, (i+1)*sendCount));
        }
        if(content.length()%sendCount!=0)
        {
            list.add(content.substring(size*sendCount, content.length()));
        }

        boolean isSendLength = false;
        for(String str : list)
        {
            ByteBuffer buffer = new ByteBuffer(Integer.MAX_VALUE/10);
            if(!isSendLength)
            {
                buffer.writeInt(content.length());
                isSendLength = true;
            }
            buffer.writeUTF(str);
            connection.sendMsg(code, buffer.getBytes());
        }
    }

    private static void battleServer(NettyConnection connection, ByteBuffer data, int code) {
        List<ServerInfoBattle> list = ServerManager.getInstance().getBattleServerList();
        JSONArray response = new JSONArray();
        JSONObject jsonObject = null;
        for (ServerInfoBattle serverInfo : list) {
            jsonObject = new JSONObject();
            jsonObject.put("id", serverInfo.getPort());
            jsonObject.put("battleCount", serverInfo.getBattleCount());
            response.add(jsonObject);
        }

        ByteBuffer out = new ByteBuffer();
        out.writeUTF(response.toJSONString());
        connection.sendMsg(code, out.getBytes());
    }

    private static void battleList(NettyConnection connection, ByteBuffer data, int code) {
        int page = data.readShort();
        int count = data.readByte();
        page = page < 1 ? 1 : page;
        count = count < 1 ? 1 : count;

        String keyword = "";
        int startTime = 0;
        int battleType = 0;
        String searchStr = data.available() > 0 ? data.readUTF() : "";
        JSONObject searchJson = null;

        if (searchStr.length() > 0) {
            searchJson = JSONObject.parseObject(searchStr);
            keyword = searchJson.getString("keyword").trim();
            startTime = searchJson.getInteger("startTime");
            battleType = searchJson.getInteger(CodeFactory.KEY_BATTLE_TYPE);
        }

        if (!StringUtils.isNullOrEmpty(keyword)) {
            TwoTuple<Player, PlayerController> playerResult = GameServer.getInstance().getWorldManager()
                    .getPlayerByName(keyword);
            if (playerResult.getFirst() != null)
                keyword = playerResult.getFirst().getObjectIndex();
        }

        List<ServerInfoBattle> serverInfoList = ServerManager.getInstance().getBattleServerList();
        List<BattleDetail> battleList = new CopyOnWriteArrayList<>();
        for (ServerInfoBattle serverInfo : serverInfoList) {
            battleList.addAll(serverInfo.getBattleList());
        }

        for (BattleDetail battleDetail : battleList) {
            if (!StringUtils.isNullOrEmpty(keyword) && !battleDetail.getTeam1Info().getIndexInfo().equals(keyword))
                battleList.remove(battleDetail);

            if (startTime > 0 && battleDetail.getCreateTime() < startTime)
                battleList.remove(battleDetail);

            if (battleType > 0) {
                if (battleType == SGCommonProto.E_BATTLE_TYPE.BATTLE_TYPE_OFFICIAL_RANK_VALUE) {
                    if (battleDetail.getBattleType() != SGCommonProto.E_BATTLE_TYPE.BATTLE_TYPE_OFFICIAL_RANK_VALUE)
                        battleList.remove(battleDetail);
                } else if (battleDetail.getBattleType() != battleType)
                    battleList.remove(battleDetail);
            }
        }

        int size = battleList.size();
        int start = (page - 1) * count;
        int end = start + count > size ? size : start + count;

        JSONObject response = new JSONObject();
        if (start < battleList.size()) {
            JSONArray jsonArray = new JSONArray();
            for (int i = start; i < end; i++) {
                // jsonArray.add(battleList.get(i).parseBase());

            }
            response.put("data", jsonArray.toJSONString());
        }

        response.put("page", page);
        response.put("count", count);
        response.put("totalCount", size);
        response.put("search", searchJson);

        ByteBuffer out = new ByteBuffer();
        out.writeUTF(response.toJSONString());
        connection.sendMsg(code, out.getBytes());
    }


    private static void feedbackList(NettyConnection connection, ByteBuffer data, int code) {
        int page = data.readShort();
        int count = data.readByte();
        page = page < 1 ? 1 : page;
        count = count < 1 ? 1 : count;

        String searchStr = data.available() > 0 ? data.readUTF() : "";
        JSONObject searchJson = null;
        String playerIndex = null;
        if (searchStr.length() > 0) {
            searchJson = JSONObject.parseObject(searchStr);
            String keyword = searchJson.getString("keyword");

            ReadPlayerMapper readPlayerMapper = SpringContext.getBean(ReadPlayerMapper.class);
            PlayerModel playerModel = readPlayerMapper.getPlayerByIndex(keyword);
            playerIndex = playerModel == null ? null : playerModel.getObjectIndex();
        }

        ReadFeedbackMapper feedBackMapper = SpringContext.getBean(ReadFeedbackMapper.class);
        int totalCount = feedBackMapper.findCount(playerIndex);
        JSONObject response = new JSONObject();
        if (totalCount > 0) {
            int start = (page - 1) * count;
            List<FeedbackModel> list = feedBackMapper.findList(start, count, playerIndex);
            JSONArray jsonArray = new JSONArray();
            for (int i = 0; i < list.size(); i++) {
                jsonArray.add(JSON.toJSON(list.get(i)));
            }
            response.put("data", jsonArray.toJSONString());
        }

        response.put("page", page);
        response.put("count", count);
        response.put("totalCount", totalCount);

        ByteBuffer out = new ByteBuffer(Integer.MAX_VALUE / 10);
        out.writeUTF(response.toJSONString());
        connection.sendMsg(code, out.getBytes());
    }

    private static void bossList(NettyConnection connection, int code)
    {
        JSONArray jsonArray = new JSONArray();

        WorldBossData bossData = GameServer.getInstance().getWorldBossData();

        List<WorldBossConfig> list = DataFactory.getInstance().getDataList(DataFactory.WORLD_BOSS_KEY);
        JSONObject jsonObject = null;
        for (WorldBossConfig bossConfig : list) {
            jsonObject = new JSONObject();
            jsonObject.put("id", bossConfig.id);
            jsonObject.put("name", bossConfig.name);
            jsonObject.put("createTime", bossConfig.freshTime.timeArray[1] + ":" + bossConfig.freshTime.timeArray[2]);
            jsonObject.put("durationTime", bossConfig.freshTime.getKeepTime(com.douqu.game.core.util.time.TimeUtils.MINUTE) + "分钟");
            if (bossData.isBossActive() && bossData.getBossInfo().getIndexInfo().equals(bossConfig.id + "")) {
                jsonObject.put("status", "攻打中");
                jsonObject.put("lv", bossData.getLv());
                jsonObject.put("hp",
                        bossData.getBossInfo().getCurrentTotalHp() + "/" + bossData.getBossInfo().getMaxTotalHp());

                JSONArray attackerArray = new JSONArray();
                JSONObject attacker = null;
                List<String> playerList = new ArrayList<>();
                List<WorldRankBean> rankBeanList = WorldInfoFactory.getInstance().getWorldRankData().getRankList(SGCommonProto.E_RANK_TYPE.RANK_TYPE_BOSS);
                for (WorldRankBean rankBean:rankBeanList){
                    playerList.add(rankBean.getObjectIndex());
                }

                if (!playerList.isEmpty()) {
                    PlayerService playerService = SpringContext.getBean(PlayerService.class);
                    List<PlayerModel> playerModelList = playerService.getPlayerListByIndex(playerList);

                    for (PlayerModel playerModel : playerModelList) {
                        attacker = new JSONObject();
                        attacker.put("name", playerModel.getName() + "(Lv" + playerModel.getLevel() + ")");
                        attacker.put("fc", playerModel.getFc());
                        int rank = rankBeanList.lastIndexOf(new WorldRankBean(playerModel.getObjectIndex()));
                        attacker.put("damage", rankBeanList.get(rank).getValue());
                        attackerArray.add(attacker);
                    }

                    jsonObject.put("players", attackerArray);
                }

            } else {
                jsonObject.put("lv", 0);
                jsonObject.put("hp", 0);

                if (bossData.getHasKillBossIntValue().contains(bossConfig.id)) {
                    jsonObject.put("status", "已通关");
                } else {
                    if (!bossConfig.isUnTouchTime(GameServer.startTime))
                        jsonObject.put("status", "已结束");
                    else
                        jsonObject.put("status", "未刷新");
                }
            }
            jsonArray.add(jsonObject);
        }

        ByteBuffer buffer = new ByteBuffer();
        buffer.writeUTF(jsonArray.toJSONString());

        connection.sendMsg(code, buffer.getBytes());
    }


    private static void auctionList(NettyConnection connection, ByteBuffer data, int code) {
        // 第几页
        int page = data.readShort();
        page = page < 1 ? 1 : page;
        // 每页大小
        int size = data.readShort();
        size = size < 1 ? 10 : size;
        JSONArray jsonArray = new JSONArray();
        // 拉取所有的拍卖品列表
        WorldAuctionData worldAuctionData = WorldInfoFactory.getInstance().getWorldAuctionData();

        Map<Integer, WorldAuctionBean> treeMap = worldAuctionData.sortAuctionMap();
        JSONObject response = new JSONObject();
        JSONObject jsonObject;
        WorldAuctionBean bean;
        int i = 0;
        int start = (page - 1) * size;
        int end = page * size;
        for (Map.Entry<Integer, WorldAuctionBean> entry : treeMap.entrySet()) {
            i++;
            // 如果大于 最大的直接break
            if (i > end) {
                break;
            }
            if (i >= start) {
                // 大于最小的才可以添加进来
                bean = entry.getValue();
                jsonObject = new JSONObject();
                jsonObject.put("id", bean.getId());
                jsonObject.put("excelId", bean.getAuctionConfigId());
                jsonObject.put("currentPrice", bean.getCurrentPrice());
                jsonObject.put("startTime", bean.getStartTime());
                jsonObject.put("state", bean.getState());
                jsonObject.put("source", E_AuctionSource.getMsgByCode(bean.getSource()));

                jsonArray.add(jsonObject);
            }
        }
        response.put("data", jsonArray);
        response.put("page", page);
        response.put("count", size);
        response.put("totalCount", treeMap.size());
        ByteBuffer byteBuffer = new ByteBuffer();
        byteBuffer.writeUTF(response.toJSONString());
        connection.sendMsg(code, byteBuffer.getBytes());

    }

    private static void auctionDetails(NettyConnection connection, ByteBuffer data, int code) {
        Integer key = data.readInt();
        if (key == null) {
            GMChannel.sendError(connection, "竞拍ID不能为空");
            return;
        }

        WorldAuctionBean bean = WorldInfoFactory.getInstance().getWorldAuctionData().getAuctionMap().get(key);
        if (bean == null) {
            GMChannel.sendError(connection, "该拍卖商品不存在");
            return;
        }
        Set<String> objectIndexSet = new HashSet<>();
        objectIndexSet.addAll(bean.getAuctionList());
        objectIndexSet.addAll(bean.getPlayerList());
        // 一次查询 且避免重复查询
        Map<String, Player> playerMap = GameServer.getInstance().getWorldManager().getPlayerMap(objectIndexSet);

        JSONObject response = new JSONObject();
        JSONArray auctionPlayers = new JSONArray();
        JSONArray sharePlarers = new JSONArray();

        for (String obejctIndex : bean.getAuctionList()) {
            auctionPlayers.add(playerMap.get(obejctIndex).getNameLv());
        }
        for (String objectIndex : bean.getPlayerList()) {
            sharePlarers.add(playerMap.get(objectIndex).getNameLv());
        }
        // 参与该竞拍的玩家
        response.put("auctionPlayer", auctionPlayers.toJSONString());
        // 参与该分红的玩家
        response.put("sharePlayer", sharePlarers.toJSONString());
        ByteBuffer byteBuffer = new ByteBuffer();
        byteBuffer.writeUTF(response.toJSONString());
        connection.sendMsg(code, byteBuffer.getBytes());
    }

    private static void cityDetail(NettyConnection connection, ByteBuffer data, int code)
    {
        int city = data.readInt();
        CityBean cityBean = WorldInfoFactory.getInstance().getWorldMapData().getCity(city);
        if (cityBean == null) {
            GMChannel.sendError(connection, "查看的城不存在");
            return;
        }

        List<SpriteBean> atkQueueList = cityBean.getSpriteBeanList(SGCommonProto.E_ARMY_TEAM.ARMY_TEAM_ATK,
                SGCommonProto.E_ARMY_STATUS.ARMY_STATUS_MATCH);
        List<SpriteBean> atkBattleList = cityBean.getSpriteBeanList(SGCommonProto.E_ARMY_TEAM.ARMY_TEAM_ATK,
                SGCommonProto.E_ARMY_STATUS.ARMY_STATUS_BATTLE);
        List<SpriteBean> defQueueList = cityBean.getSpriteBeanList(SGCommonProto.E_ARMY_TEAM.ARMY_TEAM_DEF,
                SGCommonProto.E_ARMY_STATUS.ARMY_STATUS_MATCH);
        List<SpriteBean> defBattleList = cityBean.getSpriteBeanList(SGCommonProto.E_ARMY_TEAM.ARMY_TEAM_DEF,
                SGCommonProto.E_ARMY_STATUS.ARMY_STATUS_BATTLE);
        List<SpriteBean> freeSpriteList = cityBean.getPasserbyList();

        JSONObject response = new JSONObject();
        response.put("atkQueueList", parseSpriteBeanList(atkQueueList));
        response.put("atkBattleList", parseSpriteBeanList(atkBattleList));
        response.put("defQueueList", parseSpriteBeanList(defQueueList));
        response.put("defBattleList", parseSpriteBeanList(defBattleList));
        response.put("freeSpriteList", parseSpriteBeanList(freeSpriteList));
        response.put("allPlayerList", parseSpriteBeanList(cityBean.getSpritePlayerList()));
        response.put("allGarrisonList", parseSpriteBeanList(cityBean.getSpriteGarrisonList()));
        response.put("cityName", city + " -> " + cityBean.getConfig().getName());

        ByteBuffer out = new ByteBuffer();
        out.writeUTF(response.toJSONString());
        connection.sendMsg(code, out.getBytes());
    }

    private static <T> JSONArray parseSpriteBeanList(List<T> spriteBeanList) {
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject;
        SGCommonProto.PlayerBaseInfo baseInfo;
        TwoTuple<Player, PlayerController> playerResult;
        Player player;
        SpriteBean spriteBean = null;
        for (T object : spriteBeanList) {
            jsonObject = new JSONObject();
            if (object instanceof SpriteGarrisonBean) {
                spriteBean = (SpriteGarrisonBean) object;
                jsonObject.put("objectIndex", spriteBean.getObjectIndex());
                jsonObject.put("camp", spriteBean.getCamp());
                baseInfo = spriteBean.parseBaseInfo().build();
                jsonObject.put("nickName", baseInfo.getNickName());
                jsonObject.put("lv", baseInfo.getLv());
                jsonObject.put("fc", baseInfo.getFc());
            } else if (object instanceof SpritePlayerBean) {
                spriteBean = (SpritePlayerBean) object;
                jsonObject.put("objectIndex", spriteBean.getObjectIndex());
                jsonObject.put("camp", spriteBean.getCamp());
                playerResult = GameServer.getInstance().getWorldManager().getPlayerResult(spriteBean.getObjectIndex());
                player = playerResult.getFirst();
                if (player != null) {
                    jsonObject.put("nickName", player.name);
                    jsonObject.put("lv", player.getLv());
                    jsonObject.put("fc", player.fc);
                }
            } else if(object instanceof SpriteExpeditionBean){
                spriteBean = (SpriteExpeditionBean) object;
                jsonObject.put("objectIndex", spriteBean.getObjectIndex());
                jsonObject.put("camp", spriteBean.getCamp());
                baseInfo = spriteBean.parseBaseInfo().build();
                jsonObject.put("nickName", baseInfo.getNickName());
                jsonObject.put("lv", baseInfo.getLv());
                jsonObject.put("fc", baseInfo.getFc());
            }
            jsonArray.add(jsonObject);
        }

        return jsonArray;
    }

    private static void cityList(NettyConnection connection, ByteBuffer data, int code)
    {
        WorldMapData worldMapData = WorldInfoFactory.getInstance().getWorldMapData();

        if (data.available() > 1) {
            String cmd = data.readUTF();

            if (!SpringContext.isDev()) {
                GMModel gmModel = (GMModel) connection.getObject();
                if (gmModel.getType() != E_GMType.SUPER.getCode()) {
                    GMChannel.sendError(connection, "你没有权限操作此功能");
                    return;
                }
            }

            if ("clear".equals(cmd))
            {
                worldMapData.init();
                PlayerService playerService = SpringContext.getBean(PlayerService.class);
                worldMapData.setPlayerAverageLevel(playerService.getWorldPlayerAverageLevel(10, 0));
                worldMapData.initGarrison();

                GMChannel.saveGMLog(connection, null, "清理异常大地图数据", code, cmd);
            }
            else if("fix".equals(cmd))
            {
                for(CityBean cityBean : worldMapData.getCityMap().values())
                {
                    cityBean.checkErrorData();
                }
                WorldInfoFactory.getInstance().getWorldMapData().dispatchWarn("GM已清理完异常数据,请重新进入大地图哦");

                GMChannel.sendError(connection, "已经清理完成");

                GMChannel.saveGMLog(connection, null, "清理异常大地图数据", code, cmd);
            }
            else if("fixCity".equals(cmd))
            {
                if(data.available() > 0)
                {
                    int cityId = data.readInt();
                    CityBean cityBean = worldMapData.getCity(cityId);
                    if(cityBean != null)
                    {
                        LogUtils.warn("单独修复城 -> " + cityBean.getConfig().name);
                        List<SpritePlayerBean> spritePlayerBeanList = cityBean.getSpritePlayerList();
                        for(SpritePlayerBean spritePlayerBean : spritePlayerBeanList)
                        {
                            worldMapData.backMainCity(spritePlayerBean, SGCommonProto.E_ARMY_STATUS.ARMY_STATUS_FREE, true);
                            cityBean.removeSprite(spritePlayerBean, E_CityInOutType.OUT_ERROR);
                        }
                        cityBean.setStatus(SGCommonProto.E_CITY_STATUS.CITY_STATUS_PEACE);
                        cityBean.initGarrison(E_CityInitGarrisonType.GM);
                        GMChannel.sendError(connection, "修复城完成================");

                        GMChannel.saveGMLog(connection, null, "修复城[" + cityBean.getConfig().name + "]的数据", code, cmd);
                    }
                    else
                    {
                        GMChannel.sendError(connection, "参数错误 -> " + cityId);
                    }
                }
            }
            else if("changeCityCamp".equals(cmd))
            {
//				int cityId = data.readInt();
//				CityBean cityBean = worldMapData.getCity(cityId);
//				if(cityBean == null)
//				{
//					GMChannel.sendError(connection, "城不存在 -> " + cityId);
//					return;
//				}
//
//				if(cityBean.getConfig().type == SGCommonProto.E_CITY_TYPE.CITY_TYPE_CAPITAL_VALUE)
//				{
//					GMChannel.sendError(connection, "主城不能改变");
//					return;
//				}
//
//				if(!cityBean.getSpritePlayerList().isEmpty())
//				{
//					GMChannel.sendError(connection, "请让这个城里的玩家先离开");
//					return;
//				}
//
//				if(cityBean.camp == SGCommonProto.E_CAMP_TYPE.CAMP_TYPE_BRIGHT_VALUE)
//					cityBean.camp = SGCommonProto.E_CAMP_TYPE.CAMP_TYPE_BLOODY_VALUE;
//				else if(cityBean.camp == SGCommonProto.E_CAMP_TYPE.CAMP_TYPE_BLOODY_VALUE)
//					cityBean.camp = SGCommonProto.E_CAMP_TYPE.CAMP_TYPE_DARK_VALUE;
//				else if(cityBean.camp == SGCommonProto.E_CAMP_TYPE.CAMP_TYPE_DARK_VALUE)
//					cityBean.camp = SGCommonProto.E_CAMP_TYPE.CAMP_TYPE_NEUTRAL_VALUE;
//				else if(cityBean.camp == SGCommonProto.E_CAMP_TYPE.CAMP_TYPE_NEUTRAL_VALUE)
//					cityBean.camp = SGCommonProto.E_CAMP_TYPE.CAMP_TYPE_BRIGHT_VALUE;
//
//				cityBean.setStatus(SGCommonProto.E_CITY_STATUS.CITY_STATUS_PEACE);
//				cityBean.initGarrison();

            }
        }

        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject = null;
        for (CityBean cityBean : worldMapData.getCityMap().values()) {
            jsonObject = new JSONObject();
            jsonObject.put("id", cityBean.id);
            jsonObject.put("name", cityBean.getConfig().name);
            jsonObject.put("camp", cityBean.camp);
            jsonObject.put("status", cityBean.getStatus().getNumber());
            jsonObject.put("updateTime", DataFactory.currentTime-cityBean.getLastUpdateStatusTime());
            jsonObject.put("link", Arrays.toString(cityBean.getConfig().connCityIds));
            jsonArray.add(jsonObject);
        }

        ByteBuffer out = new ByteBuffer();
        out.writeUTF(jsonArray.toJSONString());
        connection.sendMsg(code, out.getBytes());

        LogUtils.debug("返回消息 -> " + code);
    }
}
